package com.bigfreakindeals.Protocols.SoapHandlers;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Intent;
import android.graphics.Bitmap;
import android.sax.*;
import android.util.Log;
import android.util.Xml;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.bigfreakindeals.Application.ApplicationEx;
import com.bigfreakindeals.Common.Constant;
import com.bigfreakindeals.Common.Tools;
import com.bigfreakindeals.Controller.ClosestSchoolListener;
import com.bigfreakindeals.Models.UniversityDeals;
import com.bigfreakindeals.Models.UserProfile;

public class ClosestSchoolHandler  {

	// names of the XML tags
	 static final String  PARENT = "city";
	   
    static final String  ID = "id";
    static final  String NAME = "name";
    static final  String STATE = "state_name";
    static final  String SLUG = "slug";
    static final  String COUNTRY = "country_name";

    static final  String MOBILENAME= "mobile_school_title";
    static final  String LAT = "latitude";
    static final  String LONG = "longitude";
    static final  String ISAPPROVED = "is_approved";
    
    static final  String COLOR_SCHOOL_FONT = "mobileapp_school_font";
    static final  String COLOR_SCHOOL_BG = "mobileapp_background";
    static final  String COLOR_TABS_BG_NORMAL = "mobileapp_tabs_background";
    static final  String COLOR_TABS_BG_SELECTED = "mobileapp_tabs_selected_background";
    static final  String COLOR_TABS_TEXT_NORMAL = "mobileapp_tabs_font";
    static final  String COLOR_TABS_TEXT_SELECTED = "mobileapp_tabs_selected_font";
    static final  String COLOR_TITLE_BARTOP = "title_bar_top_color";
    static final  String COLOR_TITLE_BARBOTTOM = "title_bar_bottom_color";
    
    static final String FACEBOOK_URL = "facebook_url";
    static final String TWITTER_URL = "twitter_url";
    
  
    static final  String IMG_MASCOT_MAIN = "mascot_img";
    static final  String IMG_MASCOT_BEANSAY = "mobileapp_mascot_img";
    static final  String IMG_BUTTON = "mobileapp_mascot_button_img";
    static final  String IMG_BEANSAY_TEXT = "mobileapp_mascot_beans_text_img";
    static final  String IMG_TAB_REST_SMALL = "tab_rest_small_image";
    static final  String IMG_TAB_REST_LARGE = "tab_rest_large_image";
    static final  String IMG_TAB_PUSH_SMALL = "tab_push_small_image";
    static final  String IMG_TAB_PUSH_LARGE = "tab_push_large_image";
    static final  String IMG_BEANSAY_REST_SMALL = "beans_says_rest_small_image";
    static final  String IMG_BEANSAY_REST_LARGE = "beans_says_rest_large_image";
    static final  String IMG_BEANSAY_PUSH_SMALL = "beans_says_push_large_image";
    static final  String IMG_BEANSAY_PUSH_LARGE = "beans_says_push_large_image";
    
    static final  String ERROR = "error";
    UniversityDeals closestUniversity = new UniversityDeals();
    String feedUrl;
    boolean isRefresh;
    
    private static final String SOAP_ACTION = "1";
    private static final String METHOD_NAME = "bigfreakindeals";
    private static final String NAMESPACE = "1";
	ClosestSchoolListener listener;
    public ClosestSchoolHandler(String feedUrl, ClosestSchoolListener listener, boolean isRefresh) {
    	this.feedUrl = feedUrl;
    	this.listener = listener;
    	this.isRefresh = isRefresh;
    	Log.i("ClosestSchool","step 1"+feedUrl);
    	parse();
    }

    
    
    public void parse() {

     try {
    	Log.i("ClosestSchool",feedUrl);
    	SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	    envelope.setOutputSoapObject(request);
	
	    HttpTransportSE androidHttpTransport = new HttpTransportSE(feedUrl);
	    Log.i("ClosestSchool","step2");
    	
		try{
			androidHttpTransport.call(SOAP_ACTION, envelope);
		}catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
		
		SoapObject parent=(SoapObject)envelope.getResponse();
		
		SoapObject city = (SoapObject)parent.getProperty("city");
				if (city.getProperty(ID) != null) {
			String id = city.getProperty(ID).toString();
			if (id != null && id.length() > 0)
				closestUniversity.setID(id);
		}

		if (city.getProperty(NAME) != null) {
			String name = city.getProperty(NAME).toString();
			if (name != null && name.length() > 0)
				closestUniversity.setSchoolName(name);
		}
		
		if (city.getProperty(MOBILENAME) != null) {
			String mobiletitle = city.getProperty(MOBILENAME).toString();
			if (mobiletitle != null && mobiletitle.length() > 0)
				//closestUniversity.setSchoolName(mobiletitle);
				closestUniversity.setSchoolTitle(mobiletitle);
		}
		
		
		
		if (city.getProperty(STATE) != null) {
			String state = city.getProperty(STATE).toString();
			if (state != null && state.length() > 0)
				closestUniversity.setStateName(state);
		}

		if (city.getProperty(SLUG) != null) {
			String slug = city.getProperty(SLUG).toString();
			if (slug != null && slug.length() > 0) {
				closestUniversity.setSchoolSlug(slug);
			}

			System.out.println("slug"+slug);
		}
		
		if (city.getProperty("image_slug") != null) {
			String imglug = city.getProperty("image_slug").toString();
			if (imglug != null && imglug.length() > 0) {
				closestUniversity.setImageSlug(imglug);
			}

			
		}
		if (city.getProperty("image_slug_version") != null) {
			String version = city.getProperty("image_slug_version").toString();
			if (version != null && version.length() > 0) {
				closestUniversity.setImageSlugVersion(version);
			}

		}
		
		
		if (city.getProperty(COUNTRY) != null) {

			String country = city.getProperty(COUNTRY).toString();
			if (country != null && country.length() > 0)
				closestUniversity.setCountry(country);
		} else
			closestUniversity.setCountry("United States");

		if (city.getProperty(LAT) != null) {

			String lat = city.getProperty(LAT).toString();
			if (lat != null && lat.length() > 0)
				closestUniversity.setLatitude(lat);
		} else
			closestUniversity.setLatitude("1");

		if (city.getProperty(LONG) != null) {

			String longitude = city.getProperty(LONG).toString();
			if (longitude != null && longitude.length() > 0)
				closestUniversity.setLongitude(longitude);
		} else
			closestUniversity.setLongitude("1");

		if (city.getProperty(ISAPPROVED) != null) {

			String isApproved = city.getProperty(ISAPPROVED).toString();
			if (isApproved != null && isApproved.length() > 0)
				closestUniversity.setIsApproved(Integer.parseInt(isApproved));
		}

		try {
			
			closestUniversity.color_school_font = Tools.getIntValue(city.getProperty(COLOR_SCHOOL_FONT).toString());
		} catch (Exception e) {
			// TODO: handle exception
			closestUniversity.color_school_font = 0;
		}

		try {
			closestUniversity.color_school_bg = Tools.getIntValue(city.getProperty(COLOR_SCHOOL_BG).toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			closestUniversity.color_tabBGColorNormal = Tools.getIntValue(city.getProperty(COLOR_TABS_BG_NORMAL).toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			closestUniversity.color_tabFBGColorSelected = Tools.getIntValue(city.getProperty(COLOR_TABS_BG_SELECTED).toString());
		} catch (Exception e) {
			// TODO: handle exception
		}

		try {
			closestUniversity.setColor_tabFontColorNormal(Tools.getIntValue(city.getProperty(COLOR_TABS_TEXT_NORMAL).toString()));
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {

			closestUniversity.setColor_tabFontColorSelected(Tools.getIntValue(city.getProperty(COLOR_TABS_TEXT_SELECTED).toString()));
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		try {
			closestUniversity.setColor_titleBarBottomColor(Tools.getIntValue(city.getProperty(COLOR_TITLE_BARBOTTOM).toString()));
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			closestUniversity.setColor_titleBarTopColor(Tools.getIntValue(city.getProperty(COLOR_TITLE_BARBOTTOM).toString()) + 20);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		try {
			closestUniversity.setDrawable_gradient_separator(Tools.getDrawableSeparator(new int[] {
					closestUniversity.getColor_titleBarBottomColor(), closestUniversity.getColor_titleBarTopColor() }));
		} catch (Exception e) {
			// TODO: handle exception
		}

		if (city.getProperty(FACEBOOK_URL) != null) {
			String facebookURL = city.getProperty(FACEBOOK_URL).toString();
			if (facebookURL != null && facebookURL.length() > 0)
				closestUniversity.setFacebookURL(facebookURL);
		}
		if (city.getProperty(TWITTER_URL) != null) {
			String twitterURL = city.getProperty(FACEBOOK_URL).toString();
			if (twitterURL != null && twitterURL.length() > 0)
				closestUniversity.setTwitterURL(twitterURL);
		}

	
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
	
			Intent intent = new Intent(Constant.EVENTS_HTTPCONNECTION_ERROR); 
			intent.putExtra(Constant.EVENTS_HTTPCONNECTION_MESSAGE, Constant.HTTPCONNECTION_ERROR_MESSAGE);
			ApplicationEx.getInstance().sendBroadcast(intent);
			// TODO Auto-generated catch block
	
		} catch (Exception e1) {
			System.out.println("Exception "+e1.getMessage());
			Intent intent = new Intent(Constant.EVENTS_HTTPCONNECTION_ERROR); 
			intent.putExtra(Constant.EVENTS_HTTPCONNECTION_MESSAGE, Constant.HTTPCONNECTION_ERROR_MESSAGE);
			ApplicationEx.getInstance().sendBroadcast(intent);
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		
			if (  ApplicationEx.getInstance().myProfile==null)
				ApplicationEx.getInstance().myProfile = new UserProfile();
			
			System.out.println("getClosestSchool"+closestUniversity.getSchoolName());
			ApplicationEx.getInstance().myProfile.setlastSelectedSchool(closestUniversity);
			ApplicationEx.getInstance().myProfile.setHomeUniversity(closestUniversity);
			
			
			if (listener!=null)
				listener.handlerResponse(true,isRefresh);
			
			
			//get all school list as well
			if (ApplicationEx.getInstance().schoolListMap==null ||
					ApplicationEx.getInstance().schoolListMap.size() == 0){
				ApplicationEx.getInstance().controller.getSchoolListings(false)	;			
			}
			
		
    }




}

